<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 6/18/24
 * Time: 2:07 PM
 */

namespace App\Console\Commands;

use App\Services\CheckinRecordService;
use App\Services\CorpSelfService;
use Illuminate\Console\Command;
use App\Models\WechatCorpAccount as CorpAccountModel;
use Illuminate\Support\Facades\Log;


/**
 * 拉取企业微信部门成员
 *
 * Class Push
 * @package App\Console\Commands
 */
class WechatCheckinRecord extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wechat:checkin_record {checkin_type}';


    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '企业成员打卡';


    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    /**
     *
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle()
    {
        Log::info('开始添加员工打卡记录');
        $type = (int)$this->argument('checkin_type');
        $accounts = CorpAccountModel::query()->get();
        if ($accounts->isEmpty()) {

            return false;
        }
        $service = new CorpSelfService();
        $token   = $service->getAccessToken();
        foreach ($accounts as $account) {
            CheckinRecordService::checkinRecord(
                $token,
                $type,
                $account->corp_id,
                $account->department_id,
                $account->user_id,
                $account->user_name
            );
        }
    }
}
